// RenderWnd.cpp : implementation file
//

#include "stdafx.h"
#include "MFCD3D11Test.h"
#include "MainFrm.h"
#include "D3DWnd.h"
#include "RenderWnd.h"
#include "Utility/D3D/D3DUtilityFunction.h"
// CRenderWnd
IMPLEMENT_DYNAMIC(CRenderWnd, CD3DWnd)

CRenderWnd::CRenderWnd():
m_pRenderHeightMapTech(NULL),m_pXYZFile(NULL),m_bXYZLoaded(false),m_bXYZProcessed(false),m_pDirectoryUtil(NULL)
{
	InitVariables();
}
CRenderWnd::~CRenderWnd()
{
	OnDestroyResouce();
	SAFE_DELETE(m_pDirectoryUtil);
}

void CRenderWnd::InitVariables()
{
	if(m_pDirectoryUtil)
		SAFE_DELETE(m_pDirectoryUtil);
	m_pDirectoryUtil = new CDirectoryUtil();
	
	TCHAR Dir[MAX_PATH];
	ZeroMemory(Dir, sizeof(TCHAR)*MAX_PATH);

	GetModuleFileName(NULL, Dir, MAX_PATH);
	CString strDir(Dir);
	int iPos = strDir.Find('\\');
	int iPos1 = strDir.Find('\\',iPos+1);
	CString strDir1 = strDir.Left(iPos1);
	CString strDir2 = strDir.Mid(iPos,iPos1-iPos);
	strDir1 += strDir2;

	m_pDirectoryUtil->initSubDirectory(strDir1.GetBuffer());
}

HRESULT CRenderWnd::OnCreateResource(ID3D11Device* pd3dDevice)
{
	HRESULT hr = S_OK;
	InitVariables();
	return hr;
}


HRESULT CRenderWnd::OnSwapChainResize(ID3D11Device* pd3dDevice, IDXGISwapChain* pSwapChain, 
	const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc)
{
	// Setup the camera's projection parameters
	float fAspectRatio = pBackBufferSurfaceDesc->Width / ( FLOAT )pBackBufferSurfaceDesc->Height;
	m_modelViewCamera.SetProjParams( D3DX_PI / 4, fAspectRatio, 0.001f, 10000.0f );
	m_modelViewCamera.SetWindow( pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height );
	m_modelViewCamera.SetButtonMasks( MOUSE_LEFT_BUTTON, MOUSE_WHEEL, MOUSE_RIGHT_BUTTON );
	
	HRESULT hr = S_OK;
	if(m_pRenderHeightMapTech)
		hr = m_pRenderHeightMapTech->OnD3D11ResizedSwapChain(pd3dDevice,pSwapChain,pBackBufferSurfaceDesc,m_pDirectoryUtil,NULL);
	return S_OK;
}

void CRenderWnd::OnRender(ID3D11Device* pd3dDevice,ID3D11DeviceContext* pd3dDeviceContext, double fTime, float fElapsedTime)
{
	// Set vertex buffer
	if(m_pRenderHeightMapTech)
		m_pRenderHeightMapTech->OnD3D11FrameRender(pd3dDevice,pd3dDeviceContext,&m_modelViewCamera,m_pDirectoryUtil,fTime,fElapsedTime,NULL);

}

void CRenderWnd::OnFrameMove(double fTime, float fElapsedTime)
{
	m_modelViewCamera.FrameMove(fElapsedTime);
}

void CRenderWnd::OnReleasingSwapChain()
{
	if(m_pRenderHeightMapTech)
		m_pRenderHeightMapTech->OnD3D11ReleasingSwapChain(NULL);
	CD3DWnd::OnReleasingSwapChain();
}

void CRenderWnd::OnDestroyResouce()
{
	if(m_pRenderHeightMapTech)
		m_pRenderHeightMapTech->OnD3D11DestroyDevice(NULL);	
	SAFE_DELETE(m_pRenderHeightMapTech);
	CD3DWnd::OnDestroyResouce();
}


BEGIN_MESSAGE_MAP(CRenderWnd, CD3DWnd)
END_MESSAGE_MAP()



// CRenderWnd message handlers

BOOL CRenderWnd::OnWndMsg(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
	if(m_bXYZLoaded&&m_bXYZProcessed)
	{
		m_modelViewCamera.HandleMessages(GetSafeHwnd(),message,wParam,lParam);

		switch(message)
		{
		case WM_KEYDOWN:
		case WM_KEYUP:
		case WM_RBUTTONDOWN:
		case WM_MBUTTONDOWN:
		case WM_LBUTTONDOWN:
		case WM_RBUTTONDBLCLK:
		case WM_MBUTTONDBLCLK:
		case WM_LBUTTONDBLCLK:
		case WM_RBUTTONUP:
		case WM_MBUTTONUP:
		case WM_LBUTTONUP:
		case WM_CAPTURECHANGED:
		case WM_MOUSEWHEEL:
		case WM_MOUSEMOVE:
			{
				PostMessage(WM_RENDER);
				//theApp.OutputString(_T("test small window"));
			}
		}
	}
	return CD3DWnd::OnWndMsg(message, wParam, lParam, pResult);
}

HRESULT CRenderWnd::SetXYZFilePointer( CXYZFileIO* pXYZFile )
{
	m_pXYZFile = pXYZFile;
	if(m_pXYZFile==NULL)
		return E_FAIL;

	HRESULT hr = S_OK;
	if(m_pRenderHeightMapTech)
		m_pRenderHeightMapTech->OnD3D11DestroyDevice(NULL);
	else
		m_pRenderHeightMapTech = new CRenderHeightMap();
	
	m_pRenderHeightMapTech->SetXYZFilePointer(m_pXYZFile);

	ID3D11Device* pd3dDevice = AfxDXGlobal()->Device();
	hr = m_pRenderHeightMapTech->OnD3D11CreateDevice(pd3dDevice,m_pDirectoryUtil,NULL);

	// Setup the camera's view parameters
	D3DXVECTOR3 vecEye( 0.0f, 0.0f, -1.0f );
	D3DXVECTOR3 vecAt ( 0.0f, 0.0f, -0.0f );
	m_modelViewCamera.SetViewParams( &vecEye, &vecAt );

	//set process status
	//SetXYZFileProcessStatus(true);
	
	return S_OK;
}
